/*
 * Copyright (c) 2021, 2022, yzfh.cn All rights reserved.
 *
 */
package com.ly.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ly.domain.User;
import com.ly.domain.Vo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author yzfh
 * @Date 2022/02/17 星期四 14:24:28
 */
//@Controller
@RestController  //@RestController = @Controller + @ResponseBody
@RequestMapping("/user")
public class UserController {

    @RequestMapping(value = "/quick22")
    //@ResponseBody
    public void save22(String username, MultipartFile uploadFile1, MultipartFile uploadFile2) throws IOException {
        //获得上传文件的名称
        String originalFilename1 = uploadFile1.getOriginalFilename();
        uploadFile1.transferTo(new File("d:\\upload\\" + originalFilename1));
        String originalFilename2 = uploadFile2.getOriginalFilename();
        uploadFile2.transferTo(new File("d:\\upload\\" + originalFilename2));
    }

    @RequestMapping(value = "/quick21")
    //@ResponseBody
    public void save21(@CookieValue(value = "JSESSIONID", required = false) String jsessionId) {
        System.out.println(jsessionId);
    }

    @RequestMapping(value = "/quick20")
    //@ResponseBody
    public void save20(@RequestHeader(value = "User-Agent", required = false) String user_agent) {
        System.out.println(user_agent);
    }

    @RequestMapping(value = "/quick19")
    //@ResponseBody
    public void save19(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }

    @RequestMapping(value = "/quick18")
    //@ResponseBody
    public void save18(Date date) {
        System.out.println(date);
    }

    @RequestMapping(value = "/quick17/{username}")
    //@ResponseBody
    public void save17(@PathVariable(value = "username") String username) {
        System.out.println(username);
    }

    @RequestMapping(value = "/quick16")
    //@ResponseBody
    public void save16(@RequestParam(value = "name", required = false, defaultValue = "admin") String username) {
        System.out.println(username);
    }

    @RequestMapping(value = "/quick15")
    //@ResponseBody
    public void save15(@RequestBody List<User> userList) {
        System.out.println(userList);
    }

    @RequestMapping(value = "/quick14")
    //@ResponseBody
    public void save14(Vo vo) {
        System.out.println(vo);
    }

    @RequestMapping(value = "/quick13")
    //@ResponseBody
    public void save13(String[] strs) {
        System.out.println(Arrays.asList(strs));
    }

    @RequestMapping(value = "/quick12")
   // @ResponseBody
    public void save12(User user) {
        System.out.println(user);
    }

    @RequestMapping(value = "/quick11")
    //@ResponseBody
    public void save11(String username, int age) {
        System.out.println(username);
        System.out.println(age);
    }

    @RequestMapping(value = "/quick10")
    //@ResponseBody
    //期望SpringMVC自动将User转换成Json格式的字符串
    public User save10() {
        User user = new User();
        user.setUsername("wangWu");
        user.setAge(25);

        return user;
    }

    @RequestMapping(value = "/quick9")
    //@ResponseBody
    public String save9() throws JsonProcessingException {
        User user = new User();
        user.setUsername("lisi");
        user.setAge(24);
        //使用Json转换工具将对象转换成Json格式字符串在返回
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);

        return json;
    }

    @RequestMapping(value = "/quick8")
    //@ResponseBody
    public String save8() {
        return "{\"username\":\"zhangsan\",\"age\":23}";
    }

    @RequestMapping(value = "/quick7")
    //@ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String save7() {
        return "Hello Tom";
    }

    @RequestMapping(value = "/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().println("Hello Sam");
    }

    @RequestMapping(value = "/quick5")
    public String save5(HttpServletRequest request) {
        request.setAttribute("username", "Tom");
        return "success";
    }

    @RequestMapping(value = "/quick4")
    public String save4(Model model) {
        model.addAttribute("username", "李四");
        return "success";
    }

    @RequestMapping(value = "/quick3")
    public ModelAndView save3(ModelAndView modelAndView) {
        modelAndView.addObject("username", "张三");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/quick2")
    public ModelAndView save2() {
        /*
         *  Model:模型 作用封装数据
         *  View: 视图 作用展示数据
         * */
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据
        modelAndView.addObject("username", "itcast");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * 请求地址 https://localhost:3306/s3/user/quick
     *
     * @return 字符串
     */
    @RequestMapping(value = "/quick", method = RequestMethod.GET, params = "username")
    public String save() {
        System.out.println("Controller save running ....");
        return "success";
    }

}
